﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;

//for extension metods emulation on .net framework 2.0. If build target is 3.0 -> don't needed
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class ExtensionAttribute : Attribute
    {
    }
}


namespace ExcelExportLib
{

    public static class Utils
    {
        public static string GetExcelDate(this DateTime date)
        {
            /*old code: bug with some regional settings or Excel versions
             * 
             * StringBuilder sb=new StringBuilder();
            sb.Append(date.Year);
            sb.Append("-");
            sb.Append(date.Month.ToString("00"));
            sb.Append("-");
            sb.Append(date.Day.ToString("00"));
            sb.Append("T");
            string time=date.ToLongTimeString();
            if (time.IndexOf(':') == 1) //correcting 0:00:00 to 00:00:00
                sb.Append('0');
            sb.Append(time);
            return sb.ToString();*/

            return String.Format("{0:yyyy-MM-dd}T{0:HH:mm:ss.000}", date);
        }

        public static string ToIntString(this bool value)
        {
            return value ? "1" : "0";
        }
        public static string ToIntString(this bool? value)
        {
            return value.Value ? "1" : "0";
        }

        #region WriteExcelAttribute funcs
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, Color? color)
        {
            if(color.HasValue)
                writer.WriteAttributeString("ss", name, null, ColorTranslator.ToHtml(color.Value));
        }

        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, int? number)
        {
            if (number.HasValue)
                writer.WriteAttributeString("ss", name, null, number.Value.ToString());
        }

        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, double? number)
        {
            if (number.HasValue)
                writer.WriteAttributeString("ss", name, null, number.Value.ToString().Replace(',','.'));
        }

        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, bool? val)
        {
            if (val.HasValue)
                writer.WriteAttributeString("ss", name, null, val.ToIntString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, string val)
        {
            if (val!=null)
                writer.WriteAttributeString("ss", name, null, val);
        }

        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eFontUnderline? val)
        {
            if (val.HasValue)
                writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eVerticalAlignment? val)
        {
            if (val.HasValue)
                writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eHorizontalAlignment? val)
        {
            if (val.HasValue)
                writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eBorderPosition? val)
        {
            if (val.HasValue)
                writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eBackgroundPattern? val)
        {
            if (!val.HasValue) return;
            if (val == eBackgroundPattern.None) return;

            writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        public static void WriteExcelAttribute(this System.Xml.XmlWriter writer, string name, eBorderStyle? val)
        {
            if (!val.HasValue) return;
            if (val == eBorderStyle.None) return;

            writer.WriteAttributeString("ss", name, null, val.ToString());
        }
        #endregion
    }
}
